package org.jeecg.modules.rest.handle;

import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.config.thirdapp.ThirdAppTypeItemVo;
import org.springframework.web.client.HttpClientErrorException;

/**
 * 统一公共请求-农行ABC
 */
public class CommonAbcRestHandle extends CommonRestHandle{
    /**
     * 统一封装公共类POST请求
     * @param apiPath
     * @param paramObject
     * @return
     * @throws JeecgBootException
     */
    public JSONObject postRest(ThirdAppTypeItemVo config, String apiPath, JSONObject paramObject) throws JeecgBootException {
        try {
            if(null == paramObject){
                paramObject=new JSONObject();
            }
            paramObject.put("api",apiPath);
            return super.postRest(config.getOauthHost(),paramObject);
        }catch (HttpClientErrorException e){
            // 403 Forbidden: [{"request_id":"a047f1f1e41a9c205ef63242f62d0666"
            // ,"error":"error_auth","message":"Invalid access_token."}]
            String errMsg=e.getMessage();
            if(errMsg.contains("[{") && errMsg.contains("}]")){
                String errJsonStr = errMsg.substring(errMsg.indexOf("[{")
                        ,errMsg.indexOf("}]",errMsg.indexOf("[{"))+2  );
                JSONArray jsonArray = JSONArray.parseArray(errJsonStr);
                JSONObject o = (JSONObject) jsonArray.get(0);
                return o;
            }
            JSONObject o = new JSONObject();
            o.put("error","error");
            o.put("message",e.getMessage());
            return o;
        }
    }
    /**
     * 统一封装公共类GET请求
     * @param apiPath
     * @param paramObject
     * @return
     * @throws Exception
     */
    public JSONObject getRest(ThirdAppTypeItemVo config,String apiPath,JSONObject paramObject) throws JeecgBootException {
        try {
            if(null == paramObject){
                paramObject=new JSONObject();
            }
            long timest = System.currentTimeMillis() / 1000L;
            paramObject.put("time",timest);
            paramObject.put("tid",config.getClientId());
            paramObject.put("md5",MD5.create().digestHex(timest + config.getClientSecret()));
            paramObject.put("api",apiPath);

            return super.getRest(config.getOauthHost(),paramObject);
        }catch (HttpClientErrorException e){
            String errMsg=e.getMessage();
            if(errMsg.contains("[{") && errMsg.contains("}]")){
                String errJsonStr = errMsg.substring(errMsg.indexOf("[{")
                        ,errMsg.indexOf("}]",errMsg.indexOf("[{"))+2  );
                JSONArray jsonArray = JSONArray.parseArray(errJsonStr);
                JSONObject o = (JSONObject) jsonArray.get(0);
                return o;
            }
            JSONObject o = new JSONObject();
            o.put("error","error");
            o.put("message",e.getMessage());
            return o;
        }
    }
}
